<template>
  <div class="comment">
    <div class="cmt_list">
      <div
        class="cmt_list_item"
        v-for="(item, index) in commentList"
        :key="index"
      >
        <div class="item_info">
          <div class="item_info_left">
            <img
              :src="$axios.defaults.baseURL + item.account.defaultAvatar"
              alt=""
            />&nbsp;
            {{ item.account.nickname }} &nbsp;&nbsp;&nbsp;
            <span>{{ item.updated_at }}</span>
          </div>
          <div class="item_info_right">{{ item.level }}</div>
        </div>
        <div class="item_content">
          <CommentReply
            :item="item.parent"
            v-if="item.parent"
            @sendComment="CmtReply"
          />
          <div
            class="main_content"
            @mouseenter="getIndex(index)"
            @mouseleave="getIndex(null)"
          >
            {{ item.content }}
            <div class="pic_content">
              <div v-for="(item, index) in item.pics" :key="index" class="pics">
                <div class="demo-image__preview">
                  <el-image
                    style="width: 80px; height: 80px"
                    :src="$axios.defaults.baseURL + item.url"
                    :preview-src-list="[$axios.defaults.baseURL + item.url]"
                  >
                  </el-image>
                </div>
              </div>
            </div>
            <div class="cmt_reply">
              <a
                href="javascript:;"
                v-show="index == isshow"
                @click="CmtReply(item)"
                >回复</a
              >
            </div>
          </div>
        </div>
      </div>
      <div class="none_comment" v-if="commentList.length == 0">
        还没有人评论，赶快来沙发吧！
      </div>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        :total="comment.total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import Moment from "moment";
import CommentReply from "@/components/post/commentReply";

export default {
  components: {
    CommentReply,
  },

  props: ["postId", "loading"],

  data() {
    return {
      comment: {},
      commentList: [],
      isshow: null,
      pageSize: 5,
      pageIndex: 1,
    };
  },

  mounted() {
    this.getComment();
  },

  watch: {
    postId() {
      this.getComment();
    },
    loading() {
      this.getComment();
    },
  },

  methods: {
    getComment() {
      this.$axios({
        url: "/posts/comments",
        params: {
          post: this.postId,
          _limit: this.pageSize,
          _start: (this.pageIndex - 1) * this.pageSize,
        },
      }).then((res) => {
        console.log(res);
        this.commentList = res.data.data;
        this.comment = res.data;
        this.commentList.map((v) => {
          v.updated_at = Moment(v.updated_at).format("YYYY-MM-DD hh:mm");
        });
      });
    },

    // 切换条数时触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.getComment();
    },

    // 切换页数时触发
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getComment();
    },

    // 回复的显示与隐藏
    getIndex(index) {
      this.isshow = index;
    },

    // 回复
    CmtReply(item) {
      this.$emit("CmtReply", item);
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  margin-bottom: 20px;
  .cmt_list {
    width: 700px;
    border: 1px solid #ebedf0;
    border-radius: 10px;
    margin-bottom: 10px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    .cmt_list_item {
      padding: 20px 20px 5px;
      border-bottom: 1px solid #ebedf0;
      box-sizing: border-box;
      &:last-child {
        border-bottom: 0;
      }
      .item_info {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        color: #666;
        margin-bottom: 10px;
        .item_info_left {
          > img {
            width: 35px;
            height: 35px;
            vertical-align: middle;
          }
          > span {
            color: #999;
          }
        }
      }
      .item_content {
        margin-left: 30px;
        .main_content{
          margin-left: 15px;
        }
        .pic_content {
          display: flex;
          .pics {
            width: 92px;
            height: 92px;
            margin-top: 10px;
            padding: 5px;
            box-sizing: border-box;
            border-radius: 6px;
            border: 1px dashed #ddd;
            margin-right: 5px;
            img {
              display: block;
              width: 100%;
              height: 100%;
              cursor: pointer;
              object-fit: cover;
            }
          }
        }
        .cmt_reply {
          height: 20px;
          line-height: 20px;
          font-size: 14px;
          color: #1e50a2;
          text-align: right;
        }
      }
    }
  }
  .none_comment {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px 0;
  }
}
</style>